package org.aeon.client.view.components.textField;

import javax.swing.*;
import java.awt.*;

/**
 * @author WZH
 */
public class MPasswordField extends JPasswordField {
    private final String labelText;

    public MPasswordField(String labelText) {
        this.labelText = labelText;
        Insets insets = new Insets(0, 40, 0, 5);
        this.setMargin(insets);
        this.setPreferredSize(new Dimension(250, 30));
        this.setFont(new Font("黑体", Font.PLAIN, 16));
        this.setEchoChar('*');
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Insets insets = getInsets();
        int stringWidth = g.getFontMetrics().stringWidth(labelText);
        int stringHeight = g.getFontMetrics().getHeight();
        int height = this.getHeight();
        int x = (insets.left - stringWidth) / 2;
        int y = (height - stringHeight) / 2 + g.getFontMetrics().getAscent();
        g.drawString(labelText, x, y);
    }
}
